package com.qk.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.mapper.DeptMapper;
import com.qk.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class DeptServiceImpl implements DeptService {

    @Autowired
    private DeptMapper deptMapper;

    @Override
    public void saveDept(Dept dept) {
        dept.setCreateTime(LocalDateTime.now());
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.insertDept(dept);
    }

    /**
     * 查询部门列表
     *
     * @param name     部门名称
     * @param status   部门状态
     * @param page     页码
     * @param pageSize 每页数量
     * @return 部门列表
     */
    @Override
    public PageResult<Dept> pageDepts(String name, Integer status, Integer page, Integer pageSize) {
        // 分页
        PageHelper.startPage(page, pageSize);
        List<Dept> depts = deptMapper.selectDeptByCondition(name, status);
        PageInfo<Dept> pageInfo = new PageInfo<>(depts);
        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 根据部门ID查询部门
     *
     * @param id 部门ID
     * @return 部门
     */
    @Override
    public Dept getDeptById(Integer id) {
        return deptMapper.selectDeptById(id);
    }

    /**
     * 修改部门
     *
     * @param dept 部门
     */
    @Override
    public void updateDept(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.updateDeptById(dept);
    }

    /**
     * 删除部门
     *
     * @param id 部门ID
     */
    @Override
    public void removeDept(Integer id) {
        deptMapper.deleteDeptById(id);
    }

    @Override
    public List<Dept> listAllDepts() {
        return deptMapper.selectDeptByCondition(null,null);
    }
}
